<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dialogs extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();  
        $this->load->model('dialogs_mod');
    }
    
	/**
     *   
     *    
     */
    public function index( $from = NULL, $to = NULL )
    {        
        $data['menu_page'] = "dialogs"; // Koja je stranica. current menu item
        $data['from_users']= $this->dialogs_mod->getFromUsers();
        $data['from'] = $from;
        $filter = NULL;
        $data['to_users'] = NULL;
        
        if( isset($from) && $from != "" )
        {
          $filter['from'] = $from;
          $data['to_users'] = $this->dialogs_mod->getToUsers($from);
        }
        if( isset($to) && $to != "" )
          $filter['to'] = $to;
        if( $filter != NULL )
          $data['messages'] = $this->dialogs_mod->getDialog($filter);
        else
          $data['messages'] = NULL;
        // Definisi koji se view ucitava
        $data['contents'] = 'dialogs/dialogs_home';
        $this->load->view('main',$data);
    }
    
    public function filter_submit()
    {
        try{
          $filter = $this->input->post('filter');
          
          if( $filter['from'] == NULL )
            $data['messages'] = NULL;
          else
            $data['messages'] = $this->dialogs_mod->getDialog($filter);
          
          $html = $this->load->view('dialogs/dialog_list',$data,true);
          $response = array(
                        'ind' => 1,
                        'msg' => "",
                        'num_users' => 0,
                        'html' => $html
                    );
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'num_users' => 0,
                'html' => ''
            );
        }
        echo json_encode($response);
      
    }
}
?>
